import Vue from 'vue'
import App from './App.vue'
import router from './router'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import api from './api'

// 将API挂载到Vue原型上，方便全局使用
Vue.prototype.$api = api

Vue.use(ElementUI);
Vue.config.productionTip = false

// 打印当前环境信息，方便调试
// console.log('当前环境:', process.env.NODE_ENV)
// console.log('API基础路径:', process.env.VUE_APP_BASE_API)

// 注册Service Worker
if ('serviceWorker' in navigator) {
  window.addEventListener('load', async () => {
    try {
      // 使用当前域名的根路径
      const swPath = `${process.env.BASE_URL}sw.js`;
      const registration = await navigator.serviceWorker.register(swPath, {
        scope: '/'
      });
      // console.log('Service Worker 注册成功:', registration.scope);
      
      // 检查Service Worker版本
      if (registration.active) {
        const messageChannel = new MessageChannel();
        messageChannel.port1.onmessage = (event) => {
          // console.log('Service Worker 版本:', event.data);
        };
        registration.active.postMessage({ type: 'GET_VERSION' }, [messageChannel.port2]);
      }
    } catch (error) {
      // console.error('Service Worker 注册失败:', error);
    }
  });
}

new Vue({
  router,
  render: h => h(App)
}).$mount('#app')
